import pytest
from unittest.mock import patch
from fastapi.testclient import TestClient

from tldw_Server_API.app.main import app


class _QStub:
    def __init__(self, items=None):
        self._items = items or [
            {"request_id": "r1", "client_id": "c1", "priority": 3, "streaming": False, "duration": 0.01, "result": "completed", "ts": 1.0},
            {"request_id": "r2", "client_id": "c2", "priority": 2, "streaming": True,  "duration": 0.02, "result": "stream_completed", "ts": 2.0},
        ]

    def get_recent_activity(self, limit=None):
        if limit is None:
            return list(self._items)
        return list(self._items)[-int(limit):]


@pytest.mark.unit
def test_queue_activity_endpoint_enabled_default_limit():
    with TestClient(app) as client:
        with patch("tldw_Server_API.app.api.v1.endpoints.chat.get_request_queue", return_value=_QStub()):
            resp = client.get("/api/v1/chat/queue/activity")
            assert resp.status_code == 200
            data = resp.json()
            assert data.get("enabled") is True
            assert isinstance(data.get("activity"), list)
            assert data.get("limit") == 50  # default


@pytest.mark.unit
def test_queue_activity_endpoint_enabled_custom_limit():
    with TestClient(app) as client:
        with patch("tldw_Server_API.app.api.v1.endpoints.chat.get_request_queue", return_value=_QStub()):
            resp = client.get("/api/v1/chat/queue/activity?limit=2")
            assert resp.status_code == 200
            data = resp.json()
            assert data.get("enabled") is True
            assert data.get("limit") == 2
            assert len(data.get("activity")) <= 2


@pytest.mark.unit
def test_queue_activity_endpoint_disabled():
    with TestClient(app) as client:
        with patch("tldw_Server_API.app.api.v1.endpoints.chat.get_request_queue", return_value=None):
            resp = client.get("/api/v1/chat/queue/activity?limit=5")
            assert resp.status_code == 200
            data = resp.json()
            assert data.get("enabled") is False


@pytest.mark.unit
def test_queue_activity_endpoint_limit_too_large():
    with TestClient(app) as client:
        with patch("tldw_Server_API.app.api.v1.endpoints.chat.get_request_queue", return_value=_QStub()):
            resp = client.get("/api/v1/chat/queue/activity?limit=1001")
            assert resp.status_code == 400
            assert "limit" in resp.json().get("detail", "").lower()


@pytest.mark.unit
def test_queue_activity_endpoint_limit_too_small():
    with TestClient(app) as client:
        with patch("tldw_Server_API.app.api.v1.endpoints.chat.get_request_queue", return_value=_QStub()):
            resp = client.get("/api/v1/chat/queue/activity?limit=0")
            assert resp.status_code == 400
            assert "limit" in resp.json().get("detail", "").lower()
